/*     */ package tools;
/*     */
/*     */ import com.alee.laf.optionpane.WebOptionPane;
/*     */ import configs.ServerConfig;
/*     */ import handling.netty.MapleCustomEncryption;
/*     */ import java.io.BufferedInputStream;
/*     */ import java.io.DataInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.OutputStreamWriter;
/*     */ import java.io.Writer;
/*     */ import java.net.Socket;
/*     */ import java.net.URL;
/*     */ import java.net.URLConnection;
/*     */ import java.text.DateFormat;
/*     */ import java.text.ParseException;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Calendar;
/*     */ import java.util.Date;
/*     */ import java.util.Locale;
/*     */ import org.apache.logging.log4j.LogManager;
/*     */ import org.apache.logging.log4j.Logger;
/*     */ import server.ShutdownServer;
/*     */ import server.Timer;
/*     */ import server.console.Start;
/*     */
/*     */ public class Authorization {
    /*     */   public static AuthStatus sendGateMessage(String message) {
        /*  39 */     String host = "127.0.0.1";
        /*  40 */     int port = 10241;
        /*  41 */     String recv = "";
        /*     */     try {
            /*  43 */       Socket client = new Socket("127.0.0.1", 10241);
            /*  44 */       Writer writer = new OutputStreamWriter(client.getOutputStream());
            /*  45 */       writer.write(String.valueOf(message) + "\r\n");
            /*  46 */       writer.flush();
            /*  47 */       BufferedInputStream br = new BufferedInputStream(new DataInputStream(client.getInputStream()));
            /*  48 */       byte[] buffer = new byte[128];
            /*     */       int len;
            /*  50 */       while ((len = br.read(buffer)) != -1) {
                /*  51 */         byte[] decoder = new byte[len];
                /*  52 */         System.arraycopy(buffer, 0, decoder, 0, len);
                /*  53 */         MapleCustomEncryption.decryptData(decoder);
                /*  54 */         recv = new String(decoder, "GBK");
                /*     */       }
            /*  56 */       writer.close();
            /*  57 */       br.close();
            /*  58 */       client.close();
            /*  60 */     } catch (Exception e) {
            /*  61 */       log.error("与授权服务器连接失败，请查看服务器是否能打开百度网页，若无法打开请联系机房解决网络问题。", e);
            /*     */     }
        /*  63 */     String[] result = recv.split("\\|");
        /*  64 */     AuthStatus ret = null;
        /*  65 */     if (result.length == 5) {
            /*  66 */       if (!result[0].equals(String.valueOf(ServerConfig.LOGIN_MAPLE_VERSION))) {
                /*  67 */         ret = AuthStatus.AUTH_VERSION;
                /*  69 */       } else if (!result[1].equals(message)) {
                /*  70 */         ret = AuthStatus.AUTH_FAIL;
                /*  72 */       } else if (!result[2].equals(getBJTime())) {
                /*  73 */         ret = AuthStatus.AUTH_TIMEOUT;
                /*     */       } else {
                /*  76 */         AUTH_LIMIT_DAY = Integer.valueOf(result[3]).intValue() + 1;
                /*  77 */         if (AUTH_LIMIT_DAY < 4)
                    /*  78 */           WebOptionPane.showMessageDialog(null, "当前服务器还有 " + AUTH_LIMIT_DAY + "天过期，请及时续费");
                /*  80 */         ret = AuthStatus.AUTH_SUCCESS;
                /*     */       }
            /*     */     } else {
            /*  84 */       String s = recv;
            /*     */       String str1;
            /*  85 */       switch ((str1 = s).hashCode()) {
                /*     */         case -1144306145:
                    /*  85 */           if (str1.equals("Unauthorized Server.")) {
                        /*  91 */             ret = AuthStatus.AUTH_UNKNOWN;
                        /*     */             break;
                        /*     */           }
                    /*     */         case -370560810:
                    /*     */           if (str1.equals("Authorization has expired.")) {
                        /*     */             ret = AuthStatus.AUTH_TIMEOUT;
                        /*     */             break;
                        /*     */           }
                    /*     */         default:
                    /*  95 */           ret = AuthStatus.AUTH_FAIL;
                    /*     */           break;
                /*     */       }
            /*     */     }
        /*  96 */     if (result.length >
                /*     */
                /* 100 */       1) {
            /* 101 */       Date temp_date = null;
            /* 102 */       String safedate = String.valueOf(result[2].substring(0, 10));
            /* 103 */       DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            /*     */       try {
                /* 105 */         Date d = format.parse(safedate);
                /* 106 */         Calendar c = Calendar.getInstance();
                /* 107 */         c.setTime(d);
                /* 108 */         c.add(5, Integer.valueOf(result[3]).intValue() + 1);
                /* 109 */         temp_date = c.getTime();
                /* 111 */       } catch (ParseException e2) {
                /* 112 */         System.err.println("系统错误!");
                /*     */       }
            /* 114 */       AUTH_LIMIT_TIME = format.format(temp_date);
            /* 115 */       if (Start.startFinish)
                /* 116 */         Start.getInstance().updateAuthLimitTime(format.format(temp_date));
            /*     */     }
        /* 119 */     return ret;
        /*     */   }
    /*     */
    /*     */   public static String getBJTime() {
        /* 123 */     String bjTime = null;
        /* 125 */     String[] array = { "http://open.baidu.com/", "http://www.qq.com", "http://www.163.com" }, weburls = array;
        /*     */     byte b;
        /*     */     int i;
        /*     */     String[] arrayOfString1;
        /* 126 */     for (i = (arrayOfString1 = array).length, b = 0; b < i; ) {
            /* 126 */       String weburl = arrayOfString1[b];
            /*     */       try {
                /* 128 */         Locale locale = Locale.CHINA;
                /* 129 */         String pattern = "yyyy-MM-dd";
                /* 130 */         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", locale);
                /* 131 */         URL url = new URL(weburl);
                /* 132 */         URLConnection uc = url.openConnection();
                /* 133 */         uc.connect();
                /* 134 */         long ld = uc.getDate();
                /* 135 */         if (ld != 0L) {
                    /* 136 */           Date date = new Date(ld);
                    /* 137 */           bjTime = df.format(date);
                    /*     */           break;
                    /*     */         }
                /* 141 */       } catch (IOException ex) {
                /* 142 */         log.error("同步时间失败", ex);
                /*     */       }
            /*     */       b++;
            /*     */     }
        /* 145 */     return bjTime;
        /*     */   }
    /*     */
    /*     */   public static void AutoCheckAuth() {
        /* 150 */     Timer.GuiTimer.getInstance().register(() -> {
            /*     */           AuthStatus ret = checkAuth().getLeft();
            /*     */           if (ret != AuthStatus.AUTH_SUCCESS) {
                /*     */             if (failcount >= 3) {
                    /*     */               System.out.println("授权已到期");
                    /*     */               ShutdownServer.getInstance().shutdown();
                    /*     */               System.exit(0);
                    /*     */             }
                /*     */             failcount++;
                /*     */           } else {
                /*     */             Start.getInstance().updateAuthLimitTime(String.valueOf(AUTH_LIMIT_TIME) + " 授权码:" + AUTH_CODE);
                /*     */             failcount = 0;
                /*     */           }
            /* 164 */         },3600000L, 300000L);
        /*     */   }
    /*     */
    /*     */   public static Pair<AuthStatus, String> checkAuth() {
        /* 202 */     AUTH_LIMIT_TIME = "2099-01-01";
        /* 203 */     AUTH_CODE = "PPMS";
        /* 204 */     return new Pair<>(AuthStatus.AUTH_SUCCESS, AUTH_CODE);
        /*     */   }
    /*     */
    /*     */   private static int compDate(String str) {
        /*     */     try {
            /* 211 */       SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            /* 212 */       Date date = new Date();
            /* 213 */       return date.compareTo(sd.parse(str));
            /* 215 */     } catch (ParseException ex) {
            /* 216 */       return 1;
            /*     */     }
        /*     */   }
    /*     */
    /* 221 */   private static final Logger log = LogManager.getLogger();
    /*     */
    /* 222 */   public static String AUTH_LIMIT_TIME = "无";
    /*     */
    /* 223 */   public static int AUTH_LIMIT_DAY = 0;
    /*     */
    /* 224 */   private static int failcount = 0;
    /*     */
    /* 225 */   public static String AUTH_CODE = "";
    /*     */
    /*     */   public enum AuthStatus {
        /* 230 */     AUTH_SUCCESS, AUTH_TIMEOUT, AUTH_UNKNOWN, AUTH_FAIL, AUTH_VERSION, AUTH_NOCHECK;
        /*     */   }
    /*     */ }